package cn.edu.scau.cmi.pengzhijie.hibenate.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;


import cn.edu.scau.cmi.pengzhijie.hibenate.domain.Teacher;

public class TeacherDao {
	private Session session;
	private void setSession() {
			this.session = new Configuration().configure().buildSessionFactory().openSession();

	}
	
	public void save(Teacher teacher) {
		try {
			setSession();
			session.beginTransaction();
			session.save(teacher);
			session.beginTransaction().commit();
			session.close();
			System.out.println("保存成功");
		} catch (RuntimeException e) {
			// TODO: handle exception
			throw e;
		}
	}
	public void updata(Teacher teacher) {
		try {
			setSession();
			session.beginTransaction();
			session.update(teacher);
			session.beginTransaction().commit();
			session.close();
			System.out.println("修改成功");
		} catch(RuntimeException e) {
			throw e;
		}
	}
	public void delete(Teacher teacher) {
		try {
			setSession();
			session.beginTransaction();
			session.delete(teacher);
			session.beginTransaction().commit();
			session.close();
			System.out.println("删除成功");
		} catch (RuntimeException e) {
			// TODO: handle exception
			throw e;
		}
	}
	
	public List listAll() {
		try {
			setSession();
			session.beginTransaction();
			Query query =session.createQuery("from Teacher");
			List list = (List) query.list();
			session.beginTransaction().commit();
			session.close();
			return list;
		} catch (RuntimeException e) {
			// TODO: handle exception
			throw e;
		}
	}

}
